#ifndef _EQTEST_GL_TEXTURE_H
#define _EQTEST_GL_TEXTURE_H

#include <eq/gl.h>

#include "glew_context_holder.h"

namespace eqtest
{
    class TgaImage;
    
    class GLTexture : public GLEWContextHolder
    {
        friend class TextureBinding;
        
        public:
            explicit GLTexture(GLEWContext const* context);
            GLTexture(GLenum internalFormat, GLsizei width, GLsizei height, GLEWContext const* context);
            ~GLTexture();
            
            void   assign(GLenum internalFormat, GLsizei width, GLsizei height);
            GLuint get() const
            { return texture; }
            
        protected:
            void   assign(GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid const* data);
            
        private:
            void allocateDefault();
            
            GLuint texture;
    };
    
    class GLImageTexture : public GLTexture
    {
        public:
            GLImageTexture(TgaImage const& image, GLEWContext const* context);
    };
    
    class TextureBinding : public GLEWContextHolder
    {
        static GLenum const tex2enum[];
        public:
            explicit TextureBinding(GLTexture const& texture, GLuint bindTarget = 0):
            GLEWContextHolder(texture.glewGetContext())
            {
                glGetIntegerv(GL_ACTIVE_TEXTURE, &oldActive);
                glActiveTexture(tex2enum[bindTarget]);
                
                glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTexture);
                glBindTexture(GL_TEXTURE_2D, texture.get());
            }
            
            ~TextureBinding()
            {
                glBindTexture(GL_TEXTURE_2D, oldTexture);
                glActiveTexture(oldActive);
            }
            
        private:
            GLint oldActive;
            GLint oldTexture;
    };
}

#endif
